<?php

namespace App\Models\Company;

use App\Models\System\Region;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Category extends Model
{
    protected $table = 'company_category';

    protected $fillable = [
        'name',
        'uri',
        'keys',
        'title',
        'describe'
    ];

    protected $appends = [
        'replace_title',
        'replace_keys',
        'replace_describe'
//        'take_material'
    ];

    public function getReplaceTitleAttribute()
    {
        $city = is_domain_top() ? '' : config('region.region.model', '');
        if ($this->attributes['title']) {
            return $this->attributes['replace_title'] = str_replace('【标题】', $city->name ?? '', $this->attributes['title']);
        }
        return null;
    }

    public function getReplaceKeysAttribute()
    {
        $city = is_domain_top() ? '' : config('region.region.model', '');
        if ($this->attributes['keys']) {
            return $this->attributes['replace_keys'] = str_replace('【关键词】', $city->name ?? '', $this->attributes['keys']);
        }
        return null;
    }

    public function getReplaceDescribeAttribute()
    {
        $city = is_domain_top() ? '' : config('region.region.model', '');
        if ($this->attributes['describe']) {
            return $this->attributes['replace_describe'] = str_replace('【描述】', $city->name ?? '', $this->attributes['describe']);
        }
        return null;
    }

//    public function getTakeMaterialAttribute()
//    {
//        $city = config('region.region.model', '');
//        return $this->material()->with('region')->where(function ($query) use ($city) {
//            if ($city && !is_domain_top()) {
//                $query->where('region_id', $city->id);
//            }
//            $query->where('is_state', true);
//        })->orderBy('updated_at', 'desc')->take(20)->get();
//    }

    public function material()
    {
        return $this->hasMany(Material::class, 'category_id', 'id');
    }
}
